package br.com.uniban.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.com.uniban.bo.ProvaBO;
import br.com.uniban.dto.EstatisticaProvaDTO;

import com.google.gson.Gson;

public class EstatisticaAction extends DispatchAction {

	private static final String INICIO_ANALISE_PROVAS_FEITAS = "feitas";
	private static final String INICIO_ANALISE_PROVAS_CRIADAS = "criadas";
	private ProvaBO bo = null;
	
	public EstatisticaAction(){
		try {
			if (bo == null)
				bo = new ProvaBO();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ActionForward inicioProvaFeita(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		return mapping.findForward(INICIO_ANALISE_PROVAS_FEITAS);
	}
	
	public ActionForward inicioProvaCriada(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		return mapping.findForward(INICIO_ANALISE_PROVAS_CRIADAS);
	}

	public ActionForward listaProvaFeitas(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Long idUsuario = UsuarioAction.getUsuario(request).getId();
		List<EstatisticaProvaDTO> lista = bo.listaEstatisticaProvaFeita(idUsuario);
		Gson gson = new Gson();
		String json = gson.toJson(lista);
		response.getWriter().write(json);

		return null;
	}

	public ActionForward listaProvaCriadas(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Long idProva = Long.parseLong(request.getParameter("idProva"));
		EstatisticaProvaDTO estatisticaProvaCriada = bo.listaEstatisticaProvaCriada(idProva);
		Gson gson = new Gson();
		String json = gson.toJson(estatisticaProvaCriada);
		response.setContentType("application/json");
		response.getWriter().write(json);

		return null;
	}
}
